﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Emoney.DataAccess;
using LiuYan.Base;
using LiuYan.Entity;


namespace LiuYan.Service
{
    public class EvaluateService
    {
        /// <summary>
        /// 编辑评价
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Edit(TB_Evaluate entity)
        {
           SqlParameter[] parameters=new SqlParameter[]
           {
                   new SqlParameter("@Id",SqlDbType.Int), 
                   new SqlParameter("@Gid",SqlDbType.Int), 
                   new SqlParameter("@UserId",SqlDbType.Int), 
                   new SqlParameter("@Img1",SqlDbType.VarChar,50), 
                   new SqlParameter("@Img2",SqlDbType.VarChar,50),   
                   new SqlParameter("@Img3",SqlDbType.VarChar,50),   
                   new SqlParameter("@Date",SqlDbType.DateTime),   
                   new SqlParameter("@Rnk",SqlDbType.Int),   
                   new SqlParameter("@Text",SqlDbType.VarChar,500),   
                   new SqlParameter("@OrderNo",SqlDbType.VarChar,50), 
                   new SqlParameter("@PlName",SqlDbType.VarChar,150),   
           };
            parameters[0].Value = entity.Id;
            parameters[1].Value = entity.Gid;
            parameters[2].Value = entity.UserId;
            parameters[3].Value = entity.Img1;
            parameters[4].Value = entity.Img2;
            parameters[5].Value = entity.Img3;
            parameters[6].Value = entity.Date;
            parameters[7].Value = entity.Rnk;
            parameters[8].Value = entity.Text;
            parameters[9].Value = entity.OrderNo;
            parameters[10].Value = entity.PlName;
            string sql = entity.Id == 0
                ? "insert into dbo.TB_Evaluate(Gid,UserId,Img1,Img2,Img3,Rnk,Text,Del,date,orderno,PlName)values(@Gid,@UserId,@Img1,@Img2,@Img3,@Rnk,@Text,0,@Date,@OrderNo,@PlName)"
                : "update dbo.TB_Evaluate set Img1=@Img2,Img2=@Img2,Img3=@Img3,Date=@Date,Rnk=@Rnk,Text=@Text where id=@Id";
            return SqlHelper.ExecuteNonQuery(Connection.conStr, CommandType.Text, sql, parameters);
        }

        public DataSet GetList(int pageIndex, int pageCount, int gid)
        {
            string where = "";
            if (gid == 0)
            {
                where = "and Gid="+gid; 
            }
            string sql = "select COUNT(*) count from  TB_Evaluate " + (where==""?" ":" where gid="+where.Substring(0,where.Length-3))+ "  select top(" + pageCount + ")*,(select name from dbo.TB_Goods where TB_Goods.id=TB_Evaluate.Gid) as GoodsName from dbo.TB_Evaluate where id  not in (select top(" + pageIndex + ") id from TB_Evaluate order by Id desc) " + where + " order by Id desc";
            var set = SqlHelper.ExecuteDataset(Connection.conStr, CommandType.Text, sql);
            set.Tables[0].TableName = "count";
            set.Tables[1].TableName = "data";
            return set;
        }

        /// <summary>
        /// 获取商品的评价
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageCount"></param>
        /// <param name="gid"></param>
        /// <returns></returns>
        public DataSet GetGidList(int pageIndex, int pageCount,int gid)
        {
            string sql = " select COUNT(*) count from dbo.TB_Evaluate where Gid= " +gid+" "+
                         "select top(" + pageCount + ") Img1,Img2,Img3,Text,Rnk,Date,(select NcName from dbo.TB_User where id=TB_Evaluate.UserId) as Name ," +
                         "(select Img from dbo.TB_User where id=TB_Evaluate.UserId) as Img " +
                         "from dbo.TB_Evaluate where  id not in(select top("+pageIndex+")Id from dbo.TB_Evaluate order by id desc) and Gid="+gid+" order by id desc";
            var set = SqlHelper.ExecuteDataset(Connection.conStr, CommandType.Text, sql);
            set.Tables[0].TableName = "count";
            set.Tables[1].TableName = "data";
            return set;
        }

        /// <summary>
        /// 获取会员的评价
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageCount"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public DataSet GetUList(int pageIndex, int pageCount, int userId)
        {
            string sql = "select count(*) count from TB_Evaluate where userid="+userId+
                "  select top("+pageCount+") *,(select Name from dbo.TB_Goods where id=TB_Evaluate.Gid) as GoodsName " +
                         "from dbo.TB_Evaluate where  id not in(select top("+pageIndex+") Id from TB_Evaluate order by id desc) and del=0 and userid="+userId+" order by id desc";
            var set= SqlHelper.ExecuteDataset(Connection.conStr, CommandType.Text, sql);
            set.Tables[0].TableName = "count";
            set.Tables[1].TableName = "data";
            return set;
        }
        /// <summary>
        /// 验证评价
        /// </summary>
        /// <param name="id"></param>
        /// <param name="orderno"></param>
        /// <returns></returns>
        public int CheckPj(int id, string orderno)
        {
            string sql = "select count(*) from TB_Evaluate where Gid=" + id + " and OrderNo="+orderno;
            return Convert.ToInt32(SqlHelper.ExecuteScalar(Connection.conStr, CommandType.Text, sql));
        }

        //public DataSet GetList(int pageCount, int pageIndex)
        //{
        //    string sql = "select count(*) as count form TB_Goods where del=0 " +
        //                 "select top(" + pageCount + ")*,(select Name from [dbo].[TB_Goods] where id=14) as Name from [dbo].[TB_Evaluate] " +
        //                 "where Id not in(select top(" + pageIndex + ") Id from TB_Goods order by id desc) and del=0 order by id desc";

        //    var set = SqlHelper.ExecuteDataset(Connection.conStr,CommandType.Text,sql);
        //    set.Tables[0].TableName = "count";
        //    set.Tables[1].TableName = "data";
        //    return set;
        //}

        public DataSet GetList(int pageCount, int pageIndex)
        {
            string sql = "select count(*) from TB_Evaluate where del=0 " +
                         "select top(" + pageCount + ") *,(select Name from TB_Goods where Id=Gid) as Name," +
"(select Name from TB_User where TB_User.Id=TB_Evaluate.UserId) as UserName "+
"from [dbo].[TB_Evaluate] where id not in(select top(" + pageIndex + ")  Id from [dbo].[TB_Evaluate]  order by id desc)and del=0 order by id desc";
            var set = SqlHelper.ExecuteDataset(Connection.conStr, CommandType.Text, sql);
            set.Tables[0].TableName = "count";
            set.Tables[1].TableName = "data";
            return set;
        }
    }
}
